cmake_minimum_required(VERSION 3.10)
project(MyOpenGL LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")

if(MSVC)
  set(
    CMAKE_CXX_FLAGS
    "${CMAKE_CXX_FLAGS} /bigobj /MP /wd4005 /wd4996 /wd4819 /wd4251 /wd4267 /wd4244 /wd4275 /wd4530 /wd4334 /wd4018"
    )
  add_definitions(-D_ENABLE_EXTENDED_ALIGNED_STORAGE)
  add_compile_options("/utf-8")
endif(MSVC)

if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  set(CMAKE_CXX_FLAGS 
    "${CMAKE_CXX_FLAGS} -Wno-write-strings -Wno-unused-result -Wno-ignored-attributes"
  )
  set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()

option(OpenGL_PROBLEM_1 "Build problem 1" OFF)
option(OpenGL_PROBLEM_2 "Build problem 2" OFF)
option(OpenGL_PROBLEM_3 "Build problem 3" OFF)

#利用find_package命令找到OpenGL
find_package(OpenGL REQUIRED )

# 模型加载库 assimp
find_package(assimp REQUIRED)

# Eigen3
find_package(Eigen3 REQUIRED)

# GLFW
find_package(glfw3 REQUIRED)

# 添加搜索路径
include_directories(${EIGEN3_INCLUDE_DIR} ${ASSIMP_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_LIST_DIR}/depends)

add_subdirectory(utils)
add_subdirectory(depends)

# 复制model文件夹
set(MODEL ${CMAKE_CURRENT_SOURCE_DIR}/model)
file(COPY ${MODEL} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/RelWithDebInfo)
file(COPY ${MODEL} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

if(${OpenGL_PROBLEM_1})
    file(GLOB SOURCES "problem1/*.cpp")
    file(GLOB SHADERS "problem1/shader/*.vs" "problem1/shader/*.fs")
   
    add_executable(problem1 ${SOURCES})
    target_link_libraries(problem1 base glad glfw assimp::assimp Eigen3::Eigen)

    # copy files to position
    add_custom_command(
      TARGET problem1 POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E make_directory
              "$<TARGET_FILE_DIR:problem1>/shader/"
      COMMAND ${CMAKE_COMMAND} -E copy ${SHADERS}
              "$<TARGET_FILE_DIR:problem1>/shader/"
      COMMENT "Copying shader files")

      file(GLOB SOURCES "problem1.1/*.cpp")
      file(GLOB SHADERS "problem1.1/shader/*.vs" "problem1.1/shader/*.fs")

      add_executable(problem1_1 ${SOURCES})
      target_link_libraries(problem1_1 base glad glfw assimp::assimp Eigen3::Eigen)
  
      # copy files to position
      add_custom_command(
        TARGET problem1_1 POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:problem1_1>/shader/"
        COMMAND ${CMAKE_COMMAND} -E copy ${SHADERS}
                "$<TARGET_FILE_DIR:problem1_1>/shader/"
        COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_LIST_DIR}/model/"
                "$<TARGET_FILE_DIR:problem1_1>/model"
        COMMENT "Copying shader files")
      set_property(TARGET problem1 problem1_1 PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/RelWithDebInfo")
endif()

if(${OpenGL_PROBLEM_2})
    file(GLOB SOURCES "problem2/*.cpp")
    file(GLOB SHADERS "problem2/shader/*.vs" "problem2/shader/*.fs")

    add_executable(problem2 ${SOURCES})
    target_link_libraries(problem2 base glad glfw assimp::assimp Eigen3::Eigen)

    # copy files to position
    add_custom_command(
      TARGET problem2 POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E make_directory
              "$<TARGET_FILE_DIR:problem2>/shader/"
      COMMAND ${CMAKE_COMMAND} -E copy ${SHADERS}
              "$<TARGET_FILE_DIR:problem2>/shader/"
      COMMENT "Copying shader files")
    set_property(TARGET problem2 PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/RelWithDebInfo")
endif()

if(${OpenGL_PROBLEM_3})
    file(GLOB SOURCES "problem3/*.cpp")

    add_executable(problem3 ${SOURCES})
    target_link_libraries(problem3 base glad glfw assimp::assimp Eigen3::Eigen)

    add_custom_command(
      TARGET problem3 POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_LIST_DIR}/problem3/envmap/"
              "$<TARGET_FILE_DIR:problem3>/envmap/"
      COMMENT "Copying env files")
    set_property(TARGET problem3 PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/RelWithDebInfo")
endif()

if(${OpenGL_PROBLEM_4})
    file(GLOB SOURCES "problem4/*.cpp")

    add_executable(problem4 ${SOURCES})
    target_link_libraries(problem4 tinyxml2)
    set_property(TARGET problem4 PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/RelWithDebInfo")
    
endif()